namespace Domotica.Windows.Input
{
    using System;
    using System.ComponentModel;
    using System.Globalization;
    using System.Runtime.CompilerServices;

    public class SignActionConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return (sourceType == typeof(string));
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return (((destinationType == typeof(string)) && (context != null)) && ((context.Instance != null) && IsDefinedSignAction((SignAction) context.Instance)));
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source)
        {
            if ((((source == null) || !(source is string)) ? 1 : 0) != 0)
            {
                throw base.GetConvertFromException(RuntimeHelpers.GetObjectValue(source));
            }
            string str = ((string)source).Trim().ToUpper();
            if (str == string.Empty)
            {
                return SignAction.None;
            }
            return Enum.Parse(typeof(SignAction), str, true);
        }

        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == null)
            {
                throw new ArgumentNullException("destinationType");
            }
            if ((((destinationType != typeof(string)) || (value == null)) ? 1 : 0) != 0)
            {
                throw base.GetConvertToException(RuntimeHelpers.GetObjectValue(value), destinationType);
            }
            SignAction SignAction = (SignAction) value;
            if (IsDefinedSignAction(SignAction))
            {
                string str = null;
                if (SignAction == SignAction.None)
                {
                    str = string.Empty;
                }
                else
                {
                    str = SignAction.ToString();
                }
                if (str != null)
                {
                    return str;
                }
            }
            throw new InvalidEnumArgumentException("value", (int) SignAction, typeof(SignAction));
        }

        internal static bool IsDefinedSignAction(SignAction signAction)
        {
            return (signAction >= SignAction.None);
        }
    }
}

